﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.ComponentModel.Design;

namespace robotGUI
{
    /// <span class="code-SummaryComment"><summary></span>
    /// A test Control to demonstrate allowing nested Controls
    /// to accept child controls at design time.
    /// <span class="code-SummaryComment"></summary></span>
    [
    Designer(typeof(PageControlDesigner))
    ]
    public partial class PageControl : UserControl
    {
        EventHandler tabTextChanged;
        public PageControl()
        {
            InitializeComponent();
            tabTextChanged = new EventHandler(tab_TextChanged);
        }

        private void PageControl_Load(object sender, EventArgs e)
        {
            updateHeader();
        }

        private void updateHeader()
        {
            if (tabControl1.SelectedIndex != -1)
            {
                if (tabControl1.TabPages.Count > tabControl1.SelectedIndex)
                {
                    if (tabControl1.SelectedTab.Tag != null)
                        label1.Text = (string)tabControl1.SelectedTab.Tag;
                    else
                        label1.Text = tabControl1.SelectedTab.Text;
                }
                if (ImageList != null && ImageList.Images.Count > tabControl1.SelectedIndex)
                    pictureBox1.Image = ImageList.Images[tabControl1.SelectedIndex];
            }
        }

        private void populateList()
        {
            listView1.Items.Clear();
            int index = 0;
            foreach (TabPage p in tabControl1.TabPages)
            {
                p.TextChanged -= tabTextChanged;
                p.TextChanged += tabTextChanged;
                listView1.Items.Add(p.Text, index++);
            }
            if (listView1.Items.Count > 0)
                listView1.Items[tabControl1.SelectedIndex].Selected = true;
        }

        void tab_TextChanged(object sender, EventArgs e)
        {
            populateList();
            updateHeader();
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedIndices.Count > 0)
            {
                tabControl1.SelectedIndex = listView1.SelectedIndices[0];
            }
        }

        private void tabControlEx1_ControlChanged(object sender, ControlEventArgs e)
        {
            populateList();
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            updateHeader();
            listView1.Items[tabControl1.SelectedIndex].Selected = true;
        }

        [Category("Appearance")]
        public ImageList ImageList
        {
            get { return listView1.LargeImageList; }
            set { listView1.LargeImageList = value; updateHeader(); }
        }

        [Category("Appearance")]
        public Color HeadingColor
        {
            get { return headingPanel.BackColor; }
            set { headingPanel.BackColor = value; this.Invalidate(); }
        }

        [
        Category("Appearance"),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Content)
        ]
        /// <span class="code-SummaryComment"><summary></span>
        /// This property is essential to allowing the designer to work and
        /// the DesignerSerializationVisibility Attribute (above) is essential
        /// in allowing serialization to take place at design time.
        /// <span class="code-SummaryComment"></summary></span>
        public TabControlEx WorkingArea
        {
            get
            {
                return this.tabControl1;
            }
        }

    }

    //------------------------------------------------------------------------
    public class PageControlDesigner : ParentControlDesigner
    {
        public override void Initialize(System.ComponentModel.IComponent component)
        {
            base.Initialize(component);

            if (this.Control is PageControl)
            {
                this.EnableDesignMode((
                   (PageControl)this.Control).WorkingArea, "WorkingArea");
            }
        }
        protected override void WndProc(ref Message msg)
        {
            if (msg.Msg == 0x201)
            {
                if (Control is PageControl)
                {
                    PageControl cc = (PageControl)Control;
                    Point pt = cc.listView1.PointToClient(Cursor.Position);
                    ListViewHitTestInfo hit = cc.listView1.HitTest(pt);
                    if (hit.Item != null) cc.listView1.Items[hit.Item.Index].Selected = true;
                    //Console.WriteLine("Config Index " + index.ToString()); 
                }
            }
            base.WndProc(ref msg);
        }

    }

    public class TabControlEx : TabControl
    {
        /// <summary>
        /// Gets or sets a value indicating whether the tab headers should be drawn
        /// </summary>
        [
        Description("Gets or sets a value indicating whether the tab headers should be drawn"),
        DefaultValue(true)
        ]
        public bool ShowTabHeaders { get; set; }
        public TabControlEx()
            : base()
        {
        }
        protected override void WndProc(ref Message m)
        {
            //Hide tabs by trapping the TCM_ADJUSTRECT message
            if (!ShowTabHeaders && m.Msg == 0x1328)
                m.Result = (IntPtr)1;
            else
                base.WndProc(ref m);
        }
    }

}
